#include <iostream>
#include <algorithm>

using namespace std;

int main() {
    int T;
    cin >> T;

    while (T--) {
        long long n, m;
        cin >> n >> m;

        if (n == 0) {
            cout << 0 << endl;
            continue;
        }

        long long currentCapacity = 1;  // 当前产能
        long long availableFunds = 0;   // 可用资金
        long long totalTime = 0;        // 总时间

        while (true) {
            if (availableFunds >= n) {
                cout << totalTime << endl;
                break;
            }

            long long requiredFunds = n - availableFunds;
            if (requiredFunds <= 0) {
                cout << totalTime << endl;
                break;
            }

            // 计算直接生产所需时间
            long long timeToProduceDirectly = (requiredFunds + currentCapacity - 1) / currentCapacity;
            long long candidate1 = totalTime + timeToProduceDirectly;

            // 计算扩产所需时间
            long long timeToAccumulateFunds = 0;
            if (currentCapacity + 1 - availableFunds > 0) {
                timeToAccumulateFunds = (currentCapacity + 1 - availableFunds + currentCapacity - 1) / currentCapacity;
            }
            long long newCapacity = availableFunds + currentCapacity * timeToAccumulateFunds;
            long long timeToProduceAfterExpansion = 0;
            if (newCapacity > 0) {
                timeToProduceAfterExpansion = (n + newCapacity - 1) / newCapacity;
            }
            long long candidate2 = totalTime + timeToAccumulateFunds + m + timeToProduceAfterExpansion;

            if (candidate1 <= candidate2) {
                cout << candidate1 << endl;
                break;
            } else {
                totalTime += timeToAccumulateFunds + m;
                currentCapacity = newCapacity;
                availableFunds = 0;
            }
        }
    }

    return 0;
}